﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using System.Activities;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using XRM.Framework.Core;
using XRM.Solutions.FrameworkSample.Common;
using XRM.Solutions.FrameworkSample.Workflow;
using XRM.Framework.Test.Unit;

namespace XRM.Solution.FrameworkSample.UnitTests.Workflow
{
    [TestClass]
    public class TestCreditCheck : WFActivityUnitTest
    {
        #region Instance Variables

        private string creditCheckType;
        private int score;
        private Mock<ICreditCheckService> creditCheckServiceMock;

        #endregion

        #region Test

        [TestMethod]
        public void TestRunCreditCheck()
        {
            base.Test();
        }

        #endregion

        protected override Activity Setup()
        {
            creditCheckType = "Loan";
            score = 900;
            
            Entity contact = new Entity("contact");
            contact["firstname"] = "New";
            contact["lastname"] = "Customer";
            contact["birthdate"] = new DateTime(1980, 1, 1);

            OrganizationServiceMock.Setup(service =>
                                        service.Retrieve("contact",
                                                        WorkflowContext.PrimaryEntityId,
                                                        It.IsAny<ColumnSet>()))
                                    .Returns(contact);

            creditCheckServiceMock = new Mock<ICreditCheckService>();
            creditCheckServiceMock.Setup(service =>
                                        service.RunCreditCheck(creditCheckType,
                                        contact["firstname"].ToString(),
                                       contact["lastname"].ToString(),
                                       (DateTime)contact["birthdate"]))
                                .Returns(score);
            XrmContainer.Instance.Register<ICreditCheckService>(creditCheckServiceMock.Object);
            
            CreditCheck activity = new CreditCheck() { CreditCheckType = creditCheckType };
            return activity;
        }

        protected override void Verify()
        {
            Assert.IsNull(Error);

            Assert.AreEqual(score, Outputs["Score"]);

            creditCheckServiceMock.Verify();

            OrganizationServiceMock.Verify();
        }
    }
}
